﻿Imports System.ComponentModel
Imports System.Collections.ObjectModel

Public Class ObjectTree

    Public Shared ReadOnly LibraryProperty As DependencyProperty = DependencyProperty.Register("Library", GetType(RawLibraryVm), GetType(ObjectTree), New FrameworkPropertyMetadata(AddressOf OnLibraryChanged))
    Public Property Library As RawLibraryVm
        Get
            Return CType(MyBase.GetValue(LibraryProperty), RawLibraryVm)
        End Get
        Set(value As RawLibraryVm)
            MyBase.SetValue(LibraryProperty, value)
        End Set
    End Property

    Private Shared Sub OnLibraryChanged(sender As DependencyObject, e As DependencyPropertyChangedEventArgs)
        Dim tree As ObjectTree = CType(sender, ObjectTree)
        Dim library As RawLibraryVm = CType(e.NewValue, RawLibraryVm)

        Dim t As New List(Of TypeNodeVm)
        For Each desc As RawObjectDescriptor In library.RawLibrary.ObjectDescriptors.Values.OrderBy(Function(x) x.ObjectType.ToString)
            t.Add(New TypeNodeVm(desc.ObjectType, library.RawLibrary, tree))
        Next
        tree.ObjectTypeNodes.Clear()
        tree.ObjectTypeNodes.AddRange(t)
    End Sub


    Public Shared ReadOnly SelectedNodeProperty As DependencyProperty = DependencyProperty.Register("SelectedNode", GetType(NodeVm), GetType(ObjectTree), New FrameworkPropertyMetadata(AddressOf OnSelectedNodeChanged))
    Public Property SelectedNode As NodeVm
        Get
            Return CType(MyBase.GetValue(SelectedNodeProperty), NodeVm)
        End Get
        Set(value As NodeVm)
            MyBase.SetValue(SelectedNodeProperty, value)
        End Set
    End Property

    Private Shared Sub OnSelectedNodeChanged(sender As DependencyObject, e As DependencyPropertyChangedEventArgs)
        Dim tree As ObjectTree = CType(sender, ObjectTree)
        Dim node As NodeVm = CType(e.NewValue, NodeVm)
        If node IsNot Nothing Then
            tree.ContextMenuItems.Clear()
            tree.ContextMenuItems.AddRange(node.BuildContextMenu)
        End If
    End Sub


    Public Shared ReadOnly SelectedObjectProperty As DependencyProperty = DependencyProperty.Register("SelectedObject", GetType(RawObject), GetType(ObjectTree))
    Public Property SelectedObject As RawObject
        Get
            Return CType(MyBase.GetValue(SelectedObjectProperty), RawObject)
        End Get
        Set(value As RawObject)
            MyBase.SetValue(SelectedObjectProperty, value)
        End Set
    End Property

    Private mObjectTypes As New RangeObservableCollection(Of TypeNodeVm)
    Public ReadOnly Property ObjectTypeNodes As RangeObservableCollection(Of TypeNodeVm)
        Get
            Return mObjectTypes
        End Get
    End Property


    Private Sub TreeView_PreviewMouseRightButtonDown(sender As Object, e As MouseButtonEventArgs)
        Dim item As TreeViewItem = WpfUtil.FindAncestorOfType(Of TreeViewItem)(CType(e.OriginalSource, DependencyObject))
        If item IsNot Nothing Then
            item.Focus()
            e.Handled = True
        End If
    End Sub

    Private mContextMenuItems As New RangeObservableCollection(Of MenuInfo)
    Public ReadOnly Property ContextMenuItems As RangeObservableCollection(Of MenuInfo)
        Get
            Return mContextMenuItems
        End Get
    End Property
End Class
